package com.alageek.crowd.mvc.handler;

import com.alageek.crowd.entity.Auth;
import com.alageek.crowd.entity.Role;
import com.alageek.crowd.service.api.AdminService;
import com.alageek.crowd.service.api.AuthService;
import com.alageek.crowd.service.api.RoleService;
import com.alageek.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/assign")
public class AssignHandler {

    private final AdminService adminService;
    private final RoleService roleService;
    private final AuthService authService;

    public AssignHandler(RoleService roleService, AdminService adminService, AuthService authService) {
        this.roleService = roleService;
        this.adminService = adminService;
        this.authService = authService;
    }

    @ResponseBody
    @RequestMapping("/do/role/assign/auth.json")
    public ResultEntity<String> saveRoleAuthRelationship(@RequestBody Map<String, List<Integer>> map) {
        List<Integer> roleIdList = map.get("roleId");
        Integer roleId = roleIdList.get(0);
        List<Integer> authIdList = map.get("authIdArray");
        authService.saveRoleAuthRelathinship(roleId, authIdList);
        return ResultEntity.successWithoutData();
    }

    @ResponseBody
    @RequestMapping("/get/assigned/auth/id/by/role/id.json")
    public ResultEntity<List<Integer>> getAssignedAuthIdByRoleId(@RequestParam("roleId") Integer roleId) {
        List<Integer> authIdList = authService.getAssignedAuthIdByRoleId(roleId);
        return ResultEntity.successWithData(authIdList);
    }

    @ResponseBody
    @RequestMapping("/get/all/auth.json")
    public ResultEntity<List<Auth>> getAllAuth(){
        List<Auth> auths = authService.getAll();
        return ResultEntity.successWithData(auths);
    }

    @RequestMapping("/do/assign.html")
    public String saveAdminRoleRelationship(
            @RequestParam("adminId") Integer adminId,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("keyword") String keyword,
            @RequestParam(value="roleIdList", required=false) List<Integer> roleIdList){
        adminService.saveAdminRoleRelationship(adminId, roleIdList);
        return "redirect:/admin/get/page.html?pageNum="+pageNum+"&keyword="+keyword;
    }

    @RequestMapping("/get/page.html")
    public String getAssignPage(
            @RequestParam("adminId") Integer adminId,
            ModelMap modelMap){
        //查询已分配角色
        List<Role> assignedRoleList = roleService.getAssignedRole(adminId);
        //查询未分配角色
        List<Role> unAssignedRoleList = roleService.getUnAssignedRole(adminId);
        //存入模型
        modelMap.addAttribute("assignedRoleList", assignedRoleList);
        modelMap.addAttribute("unAssignedRoleList", unAssignedRoleList);
        return "assign-role";
    }

}
